const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admins'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

// 用户列表数据
const Mock = require('mockjs')
const userList = []
const count = 20

for (let i = 0; i < count; i++) {
  userList.push(Mock.mock({
    id: Mock.Random.increment(),
    username: Mock.Random.first() + Mock.Random.last(),
    password: Mock.Random.string('lower', 6, 12),
    phone: Mock.Random.string('number', 11),
    email: Mock.Random.email(),
    role: Mock.Random.pick(['admin', 'operator', 'viewer']),
    status: Mock.Random.pick([0, 1]),
    createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }))
}

module.exports = [
  // user login
  {
    url: '/vue-element-admin/user/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },

  // get user info
  {
    url: '/vue-element-admin/user/info.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/vue-element-admin/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },

  // user list
  {
    url: '/api/user/list',
    type: 'get',
    response: config => {
      const { username, phone, role, pageNum = 1, pageSize = 10 } = config.query
      let filteredList = [...userList].filter(item => {
        if (username && item.username.indexOf(username) < 0) return false
        if (phone && item.phone.indexOf(phone) < 0) return false
        if (role && item.role !== role) return false
        return true
      })

      const pageList = filteredList.slice((pageNum - 1) * pageSize, pageNum * pageSize)

      return {
        code: 200,
        message: 'success',
        data: {
          list: pageList,
          total: filteredList.length
        }
      }
    }
  },

  // add user
  {
    url: '/api/user/add',
    type: 'post',
    response: config => {
      const newUser = config.body
      newUser.id = Mock.Random.increment()
      newUser.createTime = Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
      userList.unshift(newUser)
      return {
        code: 200,
        message: '添加成功',
        data: newUser
      }
    }
  },

  // update user
  {
    url: '/api/user/update',
    type: 'post',
    response: config => {
      const updatedUser = config.body
      const index = userList.findIndex(item => item.id === updatedUser.id)
      if (index !== -1) {
        // 如果密码为空，则不更新密码
        if (!updatedUser.password) {
          updatedUser.password = userList[index].password
        }
        userList.splice(index, 1, updatedUser)
        return {
          code: 200,
          message: '修改成功',
          data: updatedUser
        }
      } else {
        return {
          code: 404,
          message: '用户不存在'
        }
      }
    }
  },

  // delete user
  {
    url: '/api/user/delete',
    type: 'post',
    response: config => {
      const { id } = config.body
      const index = userList.findIndex(item => item.id === id)
      if (index !== -1) {
        userList.splice(index, 1)
        return {
          code: 200,
          message: '删除成功'
        }
      } else {
        return {
          code: 404,
          message: '用户不存在'
        }
      }
    }
  }
]